package ru.gerunn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.zip.ZipInputStream;

import ru.gerunn.parsing.interfaces.ParseHandler;
import ru.gerunn.parsing.interfaces.Parser;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

public class LogHandler {

	private LogProvider logProvider;

	LogHandler(String dir, String prefix) throws MalformedURLException, SmbException {
		this.logProvider = new LogProvider(dir, prefix);
	}

	public void handle(Parser parser, ParseHandler parseHandler) throws IOException {
		for (SmbFile smbFile : logProvider.getFiles()) {
			ZipInputStream zipInputStream = new ZipInputStream(smbFile.getInputStream());
			zipInputStream.getNextEntry();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipInputStream, "UTF-8"));
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				parser.parse(line, parseHandler);
			}
		}
	}

}
